﻿using BusinessLayer.Biz;
using BusinessLayer.Dal;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Biz
{
    public class Product
    {
        public Product()
        {
            IsFeatured = false;
            IsDeleted = false;
        }

        [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [StringLength(250), Required]
        public string Name { get; set; }
        [Required]
        public int CurrentPrice { get; set; }
        [StringLength(5000)]
        public string ShortDescription { get; set; }        
        public Boolean IsFeatured { get; set; }
        public Boolean IsDeleted { get; set; }
        public string Description { get; set; }
        public virtual Category Category { get; set; }
        public virtual Manufacturer Manufacturer { get; set; }
        public virtual List<ProductProperties> Properties { get; set; }
        public virtual List<Comment> Comments { get; set; }
        public virtual List<ProductImage> Images { get; set; }

        ProductDal dal = new ProductDal();
        Unsold unsold = new Unsold();
        public Product GetProductById(int id)
        {
            return dal.GetProductById(id);
        }

        public int GetQuantity()
        {
            return unsold.GetQuantityByProduct(this);
        }
        public List<Product> GetAll()
        {
            return dal.GetAllProduct();
        }
        public List<Product> GetAllFeatureProduct()
        {
            return dal.GetAllFeatureProduct();
        }
        public List<Product> GetByCategory(Category category)
        {
            return dal.GetByCategory(category);
        }
        public int MaxPrice()
        {
            return dal.MaxPrice();
        }
        public List<Product> SearchByCondition(string name, int from, int to, int categoryId, int manufacturerId)
        {
            return dal.Search(name, from, to, categoryId,manufacturerId);
        }

        public override string ToString()
        {
            return Name;
        }

        public static void AddNew(Product p, List<string> imgs, List<KeyValuePair<Property, string>> props)
        {
            ProductDal.Insert(p);

            ProductImage.DeleteAllImagesOf(p);
            foreach (var i in imgs)
            {
                ProductImage img = new ProductImage();
                img.Image = i;
                img.Product = p;
                ProductImageDal.Insert(img);
            }

            ProductPropertiesDal.DeleteAllPropOf(p);
            foreach (var prop in props)
            {
                ProductProperties pp = new ProductProperties();
                pp.Product = p;
                pp.Property = prop.Key;
                pp.Value = prop.Value;
                ProductPropertiesDal.Insert(pp);
            }
        }

        public static void Delete(Product product)
        {
            ProductDal.Delete(product);
        }

        public static void Update(Product op, Product np, List<string> imgs, List<KeyValuePair<Property, string>> props)
        {
            ProductDal.Update(op, np);

            ProductImage.DeleteAllImagesOf(op);
            foreach (var i in imgs)
            {
                ProductImage img = new ProductImage();
                img.Image = i;
                img.Product = op;
                ProductImageDal.Insert(img);
            }

            ProductPropertiesDal.DeleteAllPropOf(op);
            foreach (var prop in props)
            {
                ProductProperties pp = new ProductProperties();
                pp.Product = op;
                pp.Property = prop.Key;
                pp.Value = prop.Value;
                ProductPropertiesDal.Insert(pp);
            }
        }
    }
}